package com.scb.dev.mms.controller;

import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.pojo.Position;
import com.scb.dev.mms.service.IPositionService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * ClassName: PositionController
 * Description: Position Controller
 * Date: 2018/11/27 12:08 PM
 *
 * @author Dong,Yuchuan
 * @version V1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/sm/position/")
@Api(value = "PositionController", description = "position API")
public class PositionController {

    @Autowired
    private IPositionService positionService;

    /**
     * addPosition: add position
     *
     * @param position
     * @return
     */
    @RequestMapping(value = "position", method = RequestMethod.POST)
    @ApiOperation(value="addPosition", notes="Use this API to add a new position.")
    public ResponseResult addPosition(@RequestBody @ApiParam(name="position",value="position entity")Position position, HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.insertPosition(position,current_account);
    }

    /**
     * batch add position
     * @param positionNames add position group
     * @param session
     * @return Returns the ResponseResult that adds the result
     */
    @RequestMapping(value = "position/batch",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value="addPositionBatch", notes="Use this API to batch add position.")
    public ResponseResult addPositionBatch(@RequestParam("positionNames")
                                               @ApiParam(name="positionNames",value="positionNames String")
                                                       String positionNames,
                                           HttpSession session){
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.insertPositionBatch(positionNames,current_account);
    }

    /**
     * updateEmployee: update position information
     *
     * @param position
     * @return
     */
    @RequestMapping(value = "position", method = RequestMethod.PUT)
    @ApiOperation(value="updatePosition", notes="Use this API to update a position.")
    public ResponseResult updatePosition(@RequestBody @ApiParam(name="position",value="position entity")Position position,HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.updatePosition(position,current_account);
    }

    /**
     * findOne: select all position information by positionId
     *
     * @param positionId
     * @return
     */
    @RequestMapping(value = "position/{positionId}",method = RequestMethod.GET)
    @ApiOperation(value="findOnePosition", notes="Use this API to find a position.")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType ="path",name = "positionId",value = "position id",required = true ,dataType = "String")
    })
    public ResponseResult findOne(@PathVariable("positionId") String positionId,HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.queryOne(positionId);
    }

    /**
     * findAll: select all position information detail
     *
     * @return
     */
    @RequestMapping(value = "position/detail", method = RequestMethod.GET)
    @ApiOperation(value="findAllDetail", notes="Use this API to findAllPositionDetail.")
    public ResponseResult findAllDetail(HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.queryAllPositionDetail();
    }

    /**
     * findAll: query all position
     *
     * @return
     */
    @RequestMapping(value = "position", method = RequestMethod.GET)
    @ApiOperation(value="findAll", notes="Use this API to findAllPosition.")
    public ResponseResult findAll(HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.queryAllPosition();
    }


    /**
     * deletePosition: delete position information
     *
     * @param positionId
     * @return
     */
    @RequestMapping(value = "position/{positionId}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType ="path",name = "positionId",value = "position id",required = true ,dataType = "String")
    })
    public ResponseResult deletePosition(@PathVariable("positionId") String positionId,HttpSession session) {
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return positionService.deletePosition(positionId);
    }

    /**
     * Batch delete position information
     * @param positionIds String of position information
     * @param session
     * @return
     */
    @RequestMapping(value = "position",method = RequestMethod.DELETE)
    @ApiOperation(value="deletePositions", notes="Use this API to batch delete position.")
    public ResponseResult deletePositions(@RequestBody @ApiParam(name="positionIds",value="positionIds")String positionIds,HttpSession session){
        String current_account = (String)session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(current_account)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        String positionIdStr = positionIds.substring(positionIds.indexOf(":")+2,positionIds.lastIndexOf("\""));
        return positionService.deletePositionBatch(positionIdStr);
    }



}
